package xyz.mumu233.muxinnetty.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.mumu233.muxinnetty.Enum.ResultExceptionEnum;
import xyz.mumu233.muxinnetty.bo.UserFaceImageBO;
import xyz.mumu233.muxinnetty.bo.UsersNickNameBO;
import xyz.mumu233.muxinnetty.exception.ResultException;
import xyz.mumu233.muxinnetty.pojo.Users;
import xyz.mumu233.muxinnetty.service.UserService;
import xyz.mumu233.muxinnetty.utils.FastDFSClient;
import xyz.mumu233.muxinnetty.utils.FileUtils;
import xyz.mumu233.muxinnetty.utils.MD5Utils;
import xyz.mumu233.muxinnetty.vo.JSONResultVO;
import xyz.mumu233.muxinnetty.vo.UserVO;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private FastDFSClient fastDFSClient;

    @PostMapping("/regisOrLogin")
    public JSONResultVO registOrLogin(@RequestBody Users users) throws Exception{
        if(StringUtils.isEmpty(users.getUsername()) || StringUtils.isEmpty(users.getPassword())){
            return JSONResultVO.errorMsg("用户名和密码不能为空...");
        }

        boolean usernameIsExist = userService.queryUsernameIsExist(users.getUsername());
        Users usersresult;
        if(usernameIsExist){
            usersresult = userService.queryUserForLogin(users.getUsername(),MD5Utils.getMD5Str(users.getPassword()));
            if(usersresult == null){
                return JSONResultVO.errorMsg("用户名和密码不正确");
            }
        }else{
            users.setNickname(users.getUsername());
            users.setFaceImage("");
            users.setFaceImageBig("");
            users.setPassword(MD5Utils.getMD5Str(users.getPassword()));
            usersresult = userService.save(users);
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(usersresult,userVO);
        return JSONResultVO.ok(userVO);
    }

    @PostMapping("/uploadFaceBase64")
    public JSONResultVO uploadfaceBase64(@RequestBody UserFaceImageBO userFaceImageBO) throws Exception {
        //获取前端传过来的BASE64字符串，转换为文件对象
        String bas64Data = userFaceImageBO.getFaceData();
        String userFacePath = "D:\\"+ userFaceImageBO.getUserId() + "userface64.png";
        FileUtils.base64ToFile(userFacePath,bas64Data);

        MultipartFile faceimage = FileUtils.fileToMultipart(userFacePath);

        String path = fastDFSClient.uploadBase64(faceimage);
        String[] arr = path.split("\\.");
        String url =arr[0] + "_80x80." + arr[1];

        Users users = new Users();
        users.setId(userFaceImageBO.getUserId());
        users.setFaceImage(path);
        users.setFaceImageBig(url);

        System.out.println(path);
        return JSONResultVO.ok(userService.updateUserInfo(users));
    }

    @PostMapping("/updateNickName")
    public JSONResultVO updateNickName(@RequestBody UsersNickNameBO usersNickNameBO){

        Users users = new Users();
        users.setId(usersNickNameBO.getUserId());
        users.setNickname(usersNickNameBO.getNickName());
        if (StringUtils.isEmpty(usersNickNameBO.getNickName())){
            throw new ResultException(ResultExceptionEnum.NICKNAME_ISEMPTY.getMsg());
        }
        return JSONResultVO.ok(userService.updateUserNickNmae(users));
    }

    @RequestMapping("/test")
    public JSONResultVO test(){
        return JSONResultVO.ok("测试");
    }
}
